<template>
  <el-breadcrumb separator="/" class="app-breadcrumb">
    <transition-group name="breadcrumb">
      <el-breadcrumb-item v-for="(item, index) in breadcrumb" :key="item.path">
        <span v-if="index == breadcrumb.length - 1" class="no-redirect">
          {{ item.meta.title.split('-')[0] || item.meta.title }}
        </span>
        <router-link :to="item.path" v-else>{{ item.meta.title }}</router-link>
      </el-breadcrumb-item>
    </transition-group>
  </el-breadcrumb>
</template>

<script setup>
  import { computed, onMounted, onBeforeUpdate, onBeforeUnmount } from 'vue'
  import { useRouter } from 'vue-router'
  import { useBdcStore } from '../../stores/breadcrumb.js'

  const bdcStore = useBdcStore()
  const router = useRouter()
  const breadcrumb = computed(() => {
    return router.currentRoute.value.matched
  })
  onMounted(() => {
    bdcStore.setBreadcrumb(router.currentRoute.value.matched)
  })
  onBeforeUpdate(() => {
    bdcStore.setBreadcrumb(router.currentRoute.value.matched)
  })
  onBeforeUnmount(() => {
    bdcStore.setBreadcrumb([])
  })
</script>

<style scoped>
  .app-breadcrumb.el-breadcrumb {
    display: inline-block;
    font-size: 14px;
    line-height: 50px;
    margin-left: 8px;
  }

  .no-redirect {
    color: #97a8be;
    cursor: text;
  }

  /* breadcrumb transition */
  .breadcrumb-enter-active,
  .breadcrumb-leave-active {
    transition: all 0.5s;
  }

  .breadcrumb-enter,
  .breadcrumb-leave-active {
    opacity: 0;
    transform: translateX(20px);
  }

  .breadcrumb-move {
    transition: all 0.5s;
  }

  .breadcrumb-leave-active {
    position: absolute;
  }
</style>
